﻿using System;
using System.Diagnostics;

namespace StockModel
{
    [DebuggerDisplay("Buy = {BuyDate} Sell = {SellDate} Result = {Result}")]
	[Serializable]
	public class Operation
	{
		#region Attributes

    	private readonly decimal _valueMultiplier;

    	#endregion

		#region Properties

		public decimal BuyPrice { get; set; }
		public decimal SellPrice { get; set; }
		public decimal Fee { get; set; }
		public DateTime BuyDate { get; set; }
		public DateTime SellDate { get; set; }
		public int Volume { get; set; }
		public decimal AmountRequired { get; set; }

		public decimal Result
		{
			get
			{
				return (this.SellPrice * this.Volume - this.BuyPrice * this.Volume) * _valueMultiplier - this.Fee;
			}
		}

		public decimal GrossResult
		{
			get
			{
				return (this.SellPrice * this.Volume - this.BuyPrice * this.Volume);
			}
		}

		public decimal ResultWithoutFees
    	{
    		get
    		{
				return (this.SellPrice * this.Volume - this.BuyPrice * this.Volume) * _valueMultiplier;
    		}
    	}

		public decimal ResultPercentage
		{
			get
			{
				return this.IsLongOperation ? this.Result * 100 / (this.BuyPrice * this.Volume * _valueMultiplier) : this.Result * 100 / (this.SellPrice * this.Volume * _valueMultiplier);
			}
		}

		public decimal ResultPercentageWithoutFees
		{
			get
			{
				return this.IsLongOperation ? this.ResultWithoutFees * 100 / (this.BuyPrice * this.Volume * _valueMultiplier) : this.ResultWithoutFees * 100 / (this.SellPrice * this.Volume * _valueMultiplier);
			}
		}

		public bool IsWinner
		{
			get
			{
				return this.Result > 0;
			}
        }

    	public bool IsLongOperation
    	{
    		get
    		{
				return this.SellDate > this.BuyDate;
    		}
    	}

		public DateTime ClosingDate
		{
			get
			{
				return this.SellDate > this.BuyDate ? this.SellDate : this.BuyDate;
			}
		}

		public DateTime OpeningDate
		{
			get
			{
				return this.SellDate > this.BuyDate ? this.BuyDate : this.SellDate;
			}
		}

    	#endregion

		#region Public Methods

		public Operation(decimal valueMultiplier)
		{
			_valueMultiplier = valueMultiplier;
		}

		#endregion
	}
}
